package controller;

import entity.User;
import manager.UserManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class RegisterController extends SimpleFormController {

    private UserManager userManager;

    public RegisterController() {
        setCommandClass(User.class);
        setCommandName("registerForm");
    }

    
    @Override
    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
        User user = (User) command;
        User findUser = this.userManager.searchUser(user.getName());
        if (findUser == null) {
            user.setResult("Successful");
            this.userManager.insertUser(user.getName(), user.getPass(), user.getAge(), user.getAddress(), user.getTelephone(), user.getEmail(), user.getSex());
            return new ModelAndView("registerSuccess", "customer", user);
        } else {
            user.setResult("Failed. User name is existed");
            return new ModelAndView("registerSuccess", "customer", user);
        }
    }
    

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

    public UserManager getUserManager() {
        return userManager;
    }
}
